package cn.xiongxf.web.controller;

import cn.xiongxf.context.LoginContextHolder;
import cn.xiongxf.domain.Login;
import cn.xiongxf.dto.CourseAddDto;
import cn.xiongxf.service.ICourseService;
import cn.xiongxf.domain.Course;
import cn.xiongxf.query.CourseQuery;
import cn.xiongxf.result.JsonResult;
import cn.xiongxf.result.PageList;
import cn.xiongxf.vo.CourseDataForDetailVO;
import cn.xiongxf.vo.CourseOrderDataVO;
import cn.xiongxf.vo.CourseStatusVO;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return JsonResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }

    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(courseService.selectList(null));
    }

    /**
   * 查询单个对象
   */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(courseService.selectById(id));
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
            courseService.deleteById(id);
        return JsonResult.success();
    }

    /**
    * 保存/修改对象
    */
    /*@RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.insert(course);
        }
        return JsonResult.success();
    }*/
    /**
    * 保存对象
    */
    @RequestMapping(value="/save", method= RequestMethod.POST)
    public JsonResult save(@RequestBody CourseAddDto dto){
        courseService.save(dto);
        return JsonResult.success();
    }

    /**
    * 课程上线
    */
    @PreAuthorize("hasAuthority('course:online')")
    @RequestMapping(value="/onLineCourse/{id}", method= RequestMethod.POST)
    public JsonResult onLineCourse(@PathVariable Long id){
        Login login = LoginContextHolder.getLogin();
        courseService.onLineCourse(id);
        return JsonResult.success();
    }
    /**
    * 课程下线
    */
    @RequestMapping(value="/offLineCourse/{id}", method= RequestMethod.POST)
    public JsonResult offLineCourse(@PathVariable Long id){
        courseService.offLineCourse(id);
        return JsonResult.success();
    }

    /**
     * 课程加入推荐
     */
    @RequestMapping(value="/recommendCourse/{id}", method= RequestMethod.GET)
    public JsonResult recommendCourse(@PathVariable Long id){
        courseService.recommendCourse(id);
        return JsonResult.success();
    }

    /**
     * 获取推荐课程
     */
    @RequestMapping(value="/getRecommendCourses", method= RequestMethod.GET)
    public JsonResult getRecommendCourses(){
        List<Course> recommendCourses = courseService.getRecommendCourses();
        return JsonResult.success(recommendCourses);
    }

    /**
     * 获取热门推荐课程
     */
    @RequestMapping(value="/getHotRecommendClourses", method= RequestMethod.GET)
    public JsonResult getHotRecommendClourses(){
        List<Course> hotRecommendClourses = courseService.getHotRecommendClourses();
        return JsonResult.success(hotRecommendClourses);
    }

    /**
     * 根据课程ID查询课程数据给详情页
     */
    @RequestMapping(value="/detail/data/{courseId}", method= RequestMethod.GET)
    public JsonResult selectCourseDataForDetail(@PathVariable Long courseId){
        CourseDataForDetailVO courseDataForDetailVO = courseService.selectCourseDataForDetail(courseId);
        return JsonResult.success(courseDataForDetailVO);
    }

    /**
     * 根据课程ID查询课程状态给用户
     */
    @RequestMapping(value="/status/{courseId}", method= RequestMethod.GET)
    public JsonResult selectCourseStatusForUser(@PathVariable Long courseId){
        CourseStatusVO courseStatusVO = courseService.selectCourseStatusForUser(courseId);
        return JsonResult.success(courseStatusVO);
    }

    /**
     * 根据课程ID查询课程数据给订单页
     */
    @RequestMapping(value="/orderData/{courseIdsStr}", method= RequestMethod.GET)
    public JsonResult selectCourseDataForOrder(@PathVariable String courseIdsStr){
        CourseOrderDataVO courseOrderDataVO = courseService.selectCourseDataForOrder(courseIdsStr);
        return JsonResult.success(courseOrderDataVO);
    }

}
